package flc.ast.adapter;

import android.view.View;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.stark.irremote.lib.net.bean.IrBrand;

import java.util.List;

import flc.ast.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;

public class BrandAdapter extends StkProviderMultiAdapter<IrBrand> {

    public BrandAdapter() {
        addItemProvider(new StkSingleSpanProvider(30));
        addItemProvider(new BrandProvider());
    }

    public void onSelLetter(@NonNull String letter) {
        List<IrBrand> brandList = getData();
        if (brandList == null || brandList.size() == 0) return;

        int pos = -1;
        IrBrand brand;
        for (int i = 0; i < brandList.size(); i++) {
            brand = brandList.get(i);
            if (brand.getPinYinFirstChar().equalsIgnoreCase(letter)) {
                pos = i;
                break;
            }
        }
        if (pos != -1) {
            getRecyclerView().scrollToPosition(pos);
        }
    }

    private class BrandProvider extends BaseItemProvider<IrBrand> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_brand;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, IrBrand bean) {
            baseViewHolder.setText(R.id.tvBrandItemLetter, bean.getPinYinFirstChar());
            int pos = baseViewHolder.getAdapterPosition();
            boolean showLetter = false;
            if (pos == 0) {
                showLetter = true;
            } else {
                IrBrand preBrand = getItem(pos - 1);
                if (!preBrand.getPinYinFirstChar().equalsIgnoreCase(bean.getPinYinFirstChar())) {
                    showLetter = true;
                }
            }
            baseViewHolder.getView(R.id.tvBrandItemLetter).setVisibility(showLetter ? View.VISIBLE : View.GONE);
            baseViewHolder.setText(R.id.tvBrandItemName, bean.name);
        }
    }
}
